window.onload = function() {
    var regtel = /^1[3|4|5|7|8]\d{9}$/; // 手机号码的正则表达式
    // var regqq = /^[1-9]\d{4,}$/; // 10000
    // var regnc = /^[\u4e00-\u9fa5]{2,8}$/;
    var regusername = /^[a-zA-Z0-9]{2,8}$/;
    // var regmsg = /^\d{6}$/;
    var regmsg = /^\d{4}$/;
    var regpwd = /^[a-zA-Z0-9_-]{2,16}$/;
    var tel = document.querySelector('#tel');
    // var qq = document.querySelector('#qq');
    var username = document.querySelector('#username');
    var code = document.querySelector('#code');
    var password = document.querySelector('#password');
    var surepwd = document.querySelector('#surepwd');
    regexp(tel, regtel); // 手机号码
    // regexp(qq, regqq); // qq号码
    regexp(username, regusername); // 昵称
    regexp(code, regmsg); // 短信验证
    regexp(password, regpwd); // 密码框
    // 表单验证的函数
    function regexp(ele, reg) {
        ele.onblur = function() {
            if (reg.test(this.value)) {
                // console.log('正确的');
                this.nextElementSibling.className = 'success';
                this.nextElementSibling.innerHTML = '<i class="success_icon"></i> 恭喜您输入正确';
            } else {
                // console.log('不正确');
                this.nextElementSibling.className = 'error';
                this.nextElementSibling.innerHTML = '<i class="error_icon"></i> 格式不正确，请从新输入 ';
            }
            // console.log(this)
            if(this.id=='tel'){
                if(reg.test(this.value)){
                    //通过正则
                    $('.over2').show()
                }else{
                    $('.over2').hide()
                }
            }
        }
    };

    surepwd.onblur = function() {
        if (this.value == password.value) {
            this.nextElementSibling.className = 'success';
            this.nextElementSibling.innerHTML = '<i class="success_icon"></i> 恭喜您输入正确';
        } else {
            this.nextElementSibling.className = 'error';
            this.nextElementSibling.innerHTML = '<i class="error_icon"></i> 两次密码输入不一致';

        }
    }
    $('.over2').click(()=>{
        // console.log(111)
        $.post("/api/sms/sendSms",{phone:$('#tel').val()},ret=>{
            console.log(ret)
            $('#uuid').val(ret.data.uuid)
            let allTime = 4;
            let fun = ()=>{
                let strContent = ''
                if(allTime > 0){
                    allTime--;
                    strContent += `剩余${allTime}秒`
                    $('.over2').prop('disabled','disabled')
                    $('.over2').css('background','#959A9E')
                }else{
                    strContent += '发送短信'
                    $('.over2').removeAttr('disabled')
                    $('.over2').css('background','#c81623')
                    clearInterval(timeId)
                }
                $('.over2').val(strContent)
            }
            fun()
            let timeId = setInterval(fun,1000);
        },'json')
    })

    // 注册
    $('#regForm').submit(()=>{
        let uuid = $("#uuid").val()
        let phone = $("#tel").val();
        let username = $("#username").val();
        let code = $('#code').val();
        let password = $('#password').val()
        password = md5(password)
        let jsonStr = JSON.stringify({uuid,phone,username,code,password})
        if($("#agree").prop('checked')){
            $.ajax({
                url:'/api/user/reg',
                type:'post',
                dataType:'json',
                contentType:'application/json;charset=utf-8',
                data:jsonStr,
                success:data=>{
                    // console.log(data)
                    alert(data['msg'])
                    if(data['code']==1003){
                        window.location.href = '/login.html'
                    }
                }
            })
            return false;
        }else{
            alert("未勾选协议")
            return false
        }

    })

}